



//
//  ZLearnViewController.m
//  ZQLearn
//
//  Created by admin on 2019/4/16.
//  Copyright © 2019 admin. All rights reserved.
#import "ZSearchResultVC.h"
#import "SearchBar.h"
#import "ZLearnViewController.h"
#import "ReadCollectionViewCell.h"
//#import "ZJScrollPageView.h"
#import "ZLearnSubViewController.h"
#import "ReadModel.h"
#import "StudyHardViewController.h"
@interface ZLearnViewController ()<ZJScrollPageViewDelegate>


/**
 页面分栏的标题
 */
@property (strong,nonatomic) NSArray<NSString *>*titles;

/**
 横向分页滑动组件
 */
@property (strong,nonatomic) ZJScrollPageView *scrollPageView;

@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *titlesArray;
@property (strong,nonatomic) SearchBar *searchBar;

@end

//#define kLineNumber 3

///这个cellid 全局不能重复
//NSString *const cellid = @"read";

@implementation ZLearnViewController
#pragma mark - 添加搜索框
- (void)addSearchBar{
    SearchBar *bar = [[[NSBundle mainBundle]loadNibNamed:@"SearchBar" owner:self options:nil]lastObject];
    bar.frame = CGRectMake(35, 7, ScreenWidth-70, 30);
    bar.layer.cornerRadius = 15.f;
    bar.layer.masksToBounds = YES;
    self.searchBar = bar;
    //    self.navigationItem.titleView = bar;
    [self.navigationController.navigationBar addSubview:bar];
    [bar.barBtn setTitle:@"请输入关键字搜索图书" forState:UIControlStateNormal];
    [bar.barBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
}

///搜索动作
- (void)searchAction:(UIButton *)btn{
    ZSearchResultVC *ss = [ZSearchResultVC new];
    ss.flag = @"read";
    [self.navigationController pushViewController:ss animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.searchBar.hidden = false;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.searchBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setItemWithTitle:@"读书" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.dataArray = [NSMutableArray array];
    self.titlesArray = [NSMutableArray array];
    ///请求所有标题
    [self loadData];
    self.view.backgroundColor = kWhiteColor;
    
    [self addSearchBar];
//    [self.view addSubview:self.collectionView];
    // Do any additional setup after loading the view.
}

///添加
- (void)addTabTitlesWithTitles:(NSArray *)titles{
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.scrollLineColor = NColorFrom16RGB(0x23b7e5);
    style.selectedTitleColor = NColorFrom16RGB(0x23b7e5);
    style.segmentViewBounces = YES;
    style.titleFont = kFont15;
    style.adjustCoverOrLineWidth = YES;
    style.autoAdjustTitlesWidth = YES;
    style.segmentHeight = 30;
    self.titles = titles;
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth, ScreenHeight - 20.0) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:_scrollPageView];
}

///获取所有的栏目标题
- (void)loadData{
    ///这里参数传  1 和 2 效果是一样的
    NSDictionary *dic = @{@"outsideType":@(1)};
    [ZNetworkService getRequestWithBaseUrl:kReadBookTabTitlesUrl setParameters:dic success:^(id result) {
        NSArray *arr = result[@"result"];
        for (NSDictionary *dic in arr) {
            ///字典赚模型
            
            ///注意赋值方式
            ///这种方式ID 才赋值成功  要注意因为model 里使用了mj的关键字替换方法***********
            ReadModel *model = [ReadModel mj_objectWithKeyValues:dic];
//            ReadModel *model = [[ReadModel alloc]init];
//            [model setValuesForKeysWithDictionary:dic];
            NSString *title = model.name;
            [self.titlesArray addObject:title];
            [self.dataArray addObject:model];
        }
        ///根据tab titles 来初始化tab
        [self addTabTitlesWithTitles:[self.titlesArray copy]];
        
//        [UserModel shareInstance].modelArray = self.dataArray;
        
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"jj" object:self.dataArray];
        
        YYLog(@"%@",result);
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        
    } delegater:nil];
    
}


#pragma mark ZJPhotoScrollViewDelegate
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    ReadModel *model = self.dataArray[index];
    
    [UserModel shareInstance].Id = model.ID;
    ///两种写法都可以，下面的方法可以方便的传值
    
//    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    ZLearnSubViewController *childVc = (ZLearnSubViewController *)reuseViewController;
    if (!childVc) {
        
        childVc = [[ZLearnSubViewController alloc] init];
//        childVc.Id = model.ID;
        if (self.dataArray.count>0) {
            childVc.dataArray = [self.dataArray copy];
        }
        
    }
    return childVc;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}

@end
